import java.util.ArrayList;


public class InterPolationSearch {
    public static void main(String[] args) {
        //int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        //System.out.println(binarySearch(arr, 0, arr.length - 1, 0));
        int arr[] = {1, 5, 6, 8, 9, 20, 34, 80, 99, 100, 120, 124, 155, 180, 224, 300};
        int index = interPolationSearch(arr, 0, arr.length - 1, 80);
        System.out.println(index);
    }

    public static int interPolationSearch(int[] arr, int left, int right, int value) {
        //如果左指针大于右指针时说明整个数组都没找到对应的值
        if (left > right || value < arr[0] || value > arr[arr.length - 1]) {
            return -1;
        }
        int mid = (value - arr[left]) / (arr[right] - arr[left]) * (right - left) + left;
        if (value < arr[mid]) {
            return interPolationSearch(arr, left, mid - 1, value);
        } else if (value > arr[mid]) {
            return interPolationSearch(arr, mid + 1, right, value);
        } else {
            return mid;
        }
    }

}
